package com.luolc.instantmessager.chat;
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 Luolc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.luolc.instantmessager.R;
import com.luolc.instantmessager.util.FileUtils;
import com.luolc.instantmessager.util.UiUtils;

import static com.hyphenate.easeui.ui.EaseChatFragment.EaseChatFragmentHelper;

public class ChatFragment extends EaseChatFragment implements EaseChatFragmentHelper {
  private static final int ITEM_PICTURE = 2;

  private static final int ITEM_FILE = 4;

  private static final int REQUEST_CODE_SELECT_FILE = 101;

  @Override
  protected void initView() {
    super.initView();
    //noinspection ConstantConditions
    titleBar.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.blue_series_600));
    titleBar.setRightLayoutVisibility(View.GONE);
  }

  @Override
  protected void setUpView() {
    setChatFragmentHelper(this);
    super.setUpView();
    if (chatType == EaseConstant.CHATTYPE_SINGLE) {
      titleBar.setRightLayoutVisibility(View.GONE);
    }
  }

  @Override
  public void onEnterToChatDetails() {

  }

  @Override
  protected void registerExtendMenuItem() {
    inputMenu.registerExtendMenuItem(R.string.attach_picture, R.drawable.ease_chat_image_selector,
        ITEM_PICTURE, extendMenuItemClickListener);
    inputMenu.registerExtendMenuItem(R.string.attach_file, R.drawable.em_chat_file_selector,
        ITEM_FILE, extendMenuItemClickListener);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
      switch (requestCode) {
        case REQUEST_CODE_SELECT_FILE:
          if (data != null) {
            final Uri uri = data.getData();
            if (uri != null) {
              sendFileByUri(uri);
            }
          }
          break;
      }
    }
  }

  @Override
  protected void sendFileByUri(Uri uri) {
    final String filePath = FileUtils.getFileAbsolutePath(getContext(), uri);
    if (filePath == null) {
      UiUtils.toast(getContext(), "无法选取该文件，文件可能已经损坏或不存在");
      return;
    }
    sendFileMessage(filePath);
  }

  @Override
  public boolean onExtendMenuItemClick(int itemId, View view) {
    switch (itemId) {
      case ITEM_FILE:
        selectFileFromLocal();
        break;
    }
    return false;
  }

  protected void selectFileFromLocal() {
    final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    try {
      startActivityForResult(intent, REQUEST_CODE_SELECT_FILE);
    } catch (ActivityNotFoundException ex) {
      UiUtils.toast(getContext(), "该手机尚未安装文件管理应用，无法浏览文件");
    }
  }

  @Override
  public void onSetMessageAttributes(EMMessage message) {

  }

  @Override
  public void onAvatarClick(String username) {

  }

  @Override
  public void onAvatarLongClick(String username) {

  }

  @Override
  public boolean onMessageBubbleClick(EMMessage message) {
    return false;
  }

  @Override
  public void onMessageBubbleLongClick(EMMessage message) {

  }

  @Override
  public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
    return null;
  }
}
